// .........................................................................
// Title: run_2sls.do
//
// Script to execute 2SLS estimation via Stata routines
// .........................................................................

* Set variables from arguments 
global df_path = "`1'"
global outcome = "`2'"
global regressor = "`3'"
global z_name = "`4'"
global fe_name = "`5'"
global awt_name = "`6'"
global cluster_name = "`7'"
global res_out_path = "`8'"
global sample_out_path = "`9'"

* Run estimation
use $df_path, clear
ivreghdfe $outcome ($regressor = $z_name) [aw=$awt_name], absorb($fe_name) cluster($cluster_name)

* Save sample indicators
gen _sample = e(sample)
keep _sample
save $sample_out_path, replace

* Save results
clear
set obs 1
gen beta = _b[$regressor]
gen se = _se[$regressor]
gen r2 = e(r2)
gen N = e(N)
gen F = e(rkf)
save $res_out_path, replace
